import os
import time
import allure
from selenium.webdriver.common.by import By

from web_ui_item.utils.read_data import r_data
from web_ui_item.pageobject.base_page import BasePage
from web_ui_item.utils.logger import logger

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
ELE_PATH = os.path.join(BASE_PATH, "data/elements.yaml")

class UserGroupPage(BasePage):
    _page = r_data.load_yaml(ELE_PATH)["user_group_page"]

    def add_user_group(self, group_name, desc, member):
        with allure.step("输入用户组信息，确认新增"):
            self.find(By.CSS_SELECTOR, self._page['add_user_group_btn']).click()
            self.find(By.CSS_SELECTOR, self._page['user_group_name_input']).send_keys(
                group_name)
            self.find(By.CSS_SELECTOR, self._page['user_group_desc_input']).send_keys(
                desc)
            self.find(By.CSS_SELECTOR, self._page['confirm_btn']).click()
        logger.debug("新增用户组信息")
        return UserGroupPage(self.driver)

    def update_user_group(self, group_name, new_group_name, desc):
        with allure.step("找到要修改的用户组，确认修改"):
            self.find(By.XPATH, f'//*[text()="{group_name}"]/../../i').click()
            self.find(By.CSS_SELECTOR, self._page['update_btn']).click()

            self.double_click(By.CSS_SELECTOR, self._page['user_group_name_input'])
            self.find(By.CSS_SELECTOR, self._page['user_group_name_input']).send_keys(
                new_group_name)

            self.double_click(By.CSS_SELECTOR, self._page['user_group_desc_input'])
            self.find(By.CSS_SELECTOR, self._page['user_group_desc_input']).send_keys(
                desc)
            self.find(By.CSS_SELECTOR, self._page['add_confirm_btn']).click()
        logger.debug("修改用户组信息")
        return UserGroupPage(self.driver)

    def delete_user_group(self, group_name):
        with allure.step("找到要删除的用户组，确认删除"):
            self.find(By.XPATH, f'//*[text()="{group_name[0]}"]/../../i').click()
            self.find(By.CSS_SELECTOR, self._page['delete_btn']).click()
            self.find(By.CSS_SELECTOR, self._page['confirm_btn']).click()
        logger.debug("删除用户组信息")
        return UserGroupPage(self.driver)

    def search_user_group(self, group_name):
        with allure.step("输入搜索内容，查询用户组"):
            self.find(By.CSS_SELECTOR, self._page['search_input']).send_keys(group_name)
        logger.debug("查询用户组信息")
        return UserGroupPage(self.driver)

    def get_user_group_list(self):
        '''
        获取用户组列表中所有的用户组
        :return:
        '''
        time.sleep(1)
        user_group_list = []
        eles = self.finds(By.XPATH, self._page['user_group_list'])
        for ele in eles:
            logger.debug(f'用户组列表元素：{ele}')
            user_group_list.append(ele.text)
        logger.debug(f'用户组列表中所有用户组：{user_group_list}')
        return user_group_list

    def get_empty_user_group(self):
        '''
        用户组列表暂无数据
        :return:
        '''
        ele = self.find(By.CSS_SELECTOR, self._page['empty_search'])
        value = ele.text
        return value

    def get_message_tips(self):
        time.sleep(0.5)
        ele = self.find(By.XPATH, self._page['messsge_tips'])
        value = ele.text
        return value
